% The Neurosciences Institute, 2011
%
% Project: 
%       Conscious Artifact
%
% Author:
%       Richard G. Martin
%
% Description:
%       Displays neuronal spikes generated in real-time on a raster plot.
%
% USAGE: 
%       displayRasterRT( area_cell_array, start_time, end_time )
%       Sample usage: displayRasterRT({'V1TCs', 'V1p23', 'V1p6(L4)'}, 0, 19999)
%
%
%
function displayRasterRT(area_cell_array, start_time, end_time)

spikes = []
current_spikes = [];
ms_per_sec = 1000;
% call read_groups script, assumes groups.dat has the neural area data
read_groups; 

%USAGE Test:
if nargin ~= 3
    disp('Usage: displayRasterRT( area_cell_array, start_time, end_time )')
    disp('Sample: displayRasterRT({''V1TCs'', ''V1p23'', ''V1p6(L4)''}, 0, 19999)')
    return
end

%append group names and cell types
for i=1:length(areas)
    group_name{i} = strcat(areas{i},celltype{i});
end

% If no groups are requested by user, display all groups
if( isempty(area_cell_array))%( length(area_cell_array)==0)
    area_cell_array = group_name;
end

%index all groups
for i = 1:length(area_cell_array)
	index(i) = find(strcmp( area_cell_array{i},group_name) );
    if isempty(index(i))
        disp([area_cell_array{i} ' is not in groups.dat.']);
        return;
    end
end

start_time = start_time + 1;
end_time = end_time + 1;

%find indices for all neurons from groups requested by user
for i = 1:length(area_cell_array)
	first(i) = neuron_id(index(i),1);
	last(i) = neuron_id(index(i),2);
	first(i) = first(i) + 1;
	last(i) = last(i) + 1;
	%rows(i) = la(index(i));
	%cols(i) = lb(index(i));
end

fig=figure(999);
set(fig,'DoubleBuffer','on');

time_read = start_time;
if (start_time == 1)
    start_time_sec = 1;
else
    start_time_sec = start_time/ms_per_sec;
end

end_time_sec = end_time/ms_per_sec;

%find axis Y-min and Y-max
Y_min = 0;
Y_max = 0;
for j = 1:length(area_cell_array)    
    if j == 1
        Y_max = last(j);
        Y_min = first(j);
    else
        % set min and max X-values for raster plot
        if last(j) > Y_max
            Y_max = last(j);
        end
        if first(j) < Y_min
            Y_min = first(j);
        end
    end
end

spikes = [];
% main loop: displays all spikes in raster plot
for i = start_time_sec:end_time_sec 
    filelabeltime = floor((time_read+1000)/1000)*1000;
    filename = ['spikes' num2str(filelabeltime) '.dat']
   
    %wait for spike files to be ready to read
    %TO DO: add time-out counter
    while ~exist( filename, 'file' )
        pause(.25);
    end
    
    spikes = [spikes;load(filename)];
    time_read = filelabeltime;%increment time for next file to be read
end
% end main loop

  %display user requested spikes
    for j = 1:length(area_cell_array)
        %filter out requested spikes
        %temp_spikes = current_spikes( current_spikes(:,2) >= first(j)  & current_spikes(:,2) <= last(j), :);
        %concatenate spikes for each group here
        %spikes = [spikes;temp_spikes];
    end
    
    time = spikes(:,1); % spike time
    plot(time, spikes(:,2), '.');
    axis([-Inf Inf Y_min Y_max])% appropriately scale x and y axes

